package dao;

import anno.Component;
import pojo.AttendanceRecord;
import utils.JDBCUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
public class AttendanceRecordDAO extends AbstractBaseDao<AttendanceRecord> {

    /**
     * 分页模糊查询考勤记录
     *
     * @param keyword  模糊查询关键字
     * @param pageNum  当前页码
     * @param pageSize 每页记录数
     * @return 分页查询结果
     * @throws SQLException 数据库异常
     */
    public Map<String, Object> findRecordsByPageAndKeyword(String keyword, int pageNum, int pageSize) throws SQLException {
        String sql = "SELECT * FROM tb_attendance_record WHERE record_type LIKE ? OR status LIKE ? LIMIT ?, ?";
        JDBCUtils.Page<AttendanceRecord> page = new JDBCUtils.Page<>(pageNum, pageSize);
        List<AttendanceRecord> records = JDBCUtils.executePageQuery(sql, page, this::mapResultSetToEntity, "%" + keyword + "%", "%" + keyword + "%", (pageNum - 1) * pageSize, pageSize).getList();
        long total = count();
        Map<String, Object> result = new HashMap<>();
        result.put("data", records);
        result.put("total", total);
        return result;
    }

    /**
     * 根据学生ID查询考勤记录
     *
     * @param studentId 学生ID
     * @return 考勤记录列表
     * @throws SQLException 数据库异常
     */
    public List<AttendanceRecord> findByStudentId(Integer studentId) throws SQLException {
        String sql = "SELECT * FROM tb_attendance_record WHERE student_id = ?";
        return executeQuery(sql, studentId);
    }

    /**
     * 根据考勤ID查询考勤记录
     *
     * @param attendanceId 考勤ID
     * @return 考勤记录列表
     * @throws SQLException 数据库异常
     */
    public List<AttendanceRecord> findByAttendanceId(Integer attendanceId) throws SQLException {
        String sql = "SELECT * FROM tb_attendance_record WHERE attendance_id = ?";
        return executeQuery(sql, attendanceId);
    }

    /**
     * 根据记录类型查询考勤记录
     *
     * @param recordType 记录类型
     * @return 考勤记录列表
     * @throws SQLException 数据库异常
     */
    public List<AttendanceRecord> findByRecordType(String recordType) throws SQLException {
        String sql = "SELECT * FROM tb_attendance_record WHERE record_type = ?";
        return executeQuery(sql, recordType);
    }

    /**
     * 根据状态查询考勤记录
     *
     * @param status 记录状态
     * @return 考勤记录列表
     * @throws SQLException 数据库异常
     */
    public List<AttendanceRecord> findByStatus(String status) throws SQLException {
        String sql = "SELECT * FROM tb_attendance_record WHERE status = ?";
        return executeQuery(sql, status);
    }

    /**
     * 根据时间段查询考勤记录
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return 考勤记录列表
     * @throws SQLException 数据库异常
     */
    public List<AttendanceRecord> findByTimeRange(LocalDateTime startTime, LocalDateTime endTime) throws SQLException {
        String sql = "SELECT * FROM tb_attendance_record WHERE check_in_time BETWEEN ? AND ?";
        return executeQuery(sql, startTime, endTime);
    }

    /**
     * 执行查询并返回结果列表
     *
     * @param sql    查询SQL
     * @param params 查询参数
     * @return 结果列表
     * @throws SQLException 数据库异常
     */
    private List<AttendanceRecord> executeQuery(String sql, Object... params) throws SQLException {
        ResultSet rs = (ResultSet) JDBCUtils.execute(sql, params);
        List<AttendanceRecord> resultList = new ArrayList<>();
        while (rs.next()) {
            resultList.add(mapResultSetToEntity(rs));
        }
        return resultList;
    }
}